// myMixin.js
import {
	emojiList,
	replaceEmoji
} from '@/common/emoji.js'
import {
	userShareRecord
} from '@/common/http/api.js'
export const globalMixin = {
	data() {
		return {
			ossUrl: '',
			mixinUserInfo: ''
		};
	},
	created() {

		this.ossUrl = this.$store.state.ossurl

	},
	onShareAppMessage() {
		let img = ''
		let title = ''
		let path = '/pages/loading/loading'
		if (this.isValid(this.$store.state.share.share_title)) {
			title = this.$store.state.share.share_title
			img = this.$store.state.share.intercept ? this.$store.state.share.intercept : this.$store.state.share.share_img
			path += '?shareId=' + this.$store.state.share.business_card_id
			// if (this.$store.state.userInfo.id) {
			// 	path += '?shareId=' + this.$store.state.share.business_card_id + '&userId=' + this.$store.state
			// 		.userInfo.id
			// } else {
			// 	path += '?shareId=' + this.$store.state.share.business_card_id
			// }
			let token = uni.getStorageSync('token')
			if(token) {
				let data = {
					my: this.$store.state.userInfo.id,
					you: this.$store.state.share.user_id,
					business_card_id: this.$store.state.share.business_card_id
				}
				userShareRecord(data)
			}
			
		}

		return {
			title,
			path,
			imageUrl: img
		};
	},

	//收藏
	onAddToFavorites(res) {
		console.log('收藏mixin', this.$store.state)
		let img = ''
		let title = ''
		let path = `is_mixin_collect=1&`
		if (this.isValid(this.$store.state.share.share_title)) {
			title = this.$store.state.share.share_title
			img = this.$store.state.share.intercept ? this.$store.state.share.intercept : this.$store.state.share.share_img
			path += 'shareId=' + this.$store.state.share.business_card_id
			// if (this.$store.state.userInfo.id) {
			// 	path += '?shareId=' + this.$store.state.share.business_card_id + '&userId=' + this.$store.state
			// 		.userInfo.id
			// } else {
			// 	path += '?shareId=' + this.$store.state.share.business_card_id
			// }
			let token = uni.getStorageSync('token')
			if(token) {
				let data = {
					my: this.$store.state.userInfo.id,
					you: this.$store.state.share.user_id,
					business_card_id: this.$store.state.share.business_card_id
				}
				userShareRecord(data)
			}

		}
		return {
			title,
			query: path,
			imageUrl: img
		};
	},
	onLoad(e){
		console.log('mixin', e, wx.getLaunchOptionsSync())
		const obj = wx.getLaunchOptionsSync()
		
		
		//判断是否收藏  
		if(e.is_mixin_collect && obj.scene != 1154) {
			//收藏
			if(e.is_mixin_collect) {
				console.log('收藏')
				// 开启 不请求接口
				this.$store.commit('setIsRequest', true)
				//判断登录
				let token = uni.getStorageSync('token')
				if(!token) {
					this.$store.dispatch('login', e).then(()=>{
						this.$store.dispatch('handel', e)
					}).catch(()=>{
						this.$u.toast('登录失败！')
					})
					
				} else {
					this.$store.dispatch('handel', e)
				}
			}
		}
	},
	methods: {
		
		
		isValid(value) {
			if (Array.isArray(value) && value.length === 0) {
				return false
			}
			return value !== undefined && value !== null && value !== '';
		},
		// 点击图片预览时触发的方法
		MpreviewImage(arr,index = 0) {
			// UniApp中调用微信小程序原生API
			uni.previewImage({
				current: arr[index], // 当前显示图片的链接
				urls: arr // 需要预览的图片链接列表
			});
		},
		getAuth(type = false) {
			let token = uni.getStorageSync('token')
			if (token) {
				return true
			}
			if (type) {

				uni.navigateTo({
					url: '/pages/login/login'
				})
			}
			return false
		},
		toMixinIndex(type, query) {
			console.log('type,query', type, query)
			// 1 首页
			switch (type) {
				case 1:
				// this.$store.commit('setIsRefresh', true)
					uni.navigateTo({
						url: '/pages/index/other?current=0' + '&userId=' + query.userId
					})
					break;
				case 2:
				// this.$store.commit('setIsRefresh', true)
					if(query.type === 2) {
						uni.navigateTo({
							url: '/pages/index/other?isBackHome=2&shareId=' + query.business_card_id
						})
					} else {
						uni.navigateTo({
							url: '/pages/index/other?isBackHome=2&current=0' + '&userId=' + query.userId
						})
					}
					
					break;
				case 3:
				// this.$store.commit('setIsRefresh', true)
					uni.navigateTo({
						url: '/pages/index/other?isBackHome=2&shareId=' + query.business_card_id
					})
					break;
				default:
					break;
			}
		},
		//替换表情符号为图片
		replaceEmoji
	}
};